<?php

namespace app\common\service;


use app\common\model\CmsArticleClass;
use app\common\model\Forum;
use app\common\util\Result;
use think\facade\Request;
use think\facade\Validate;
use think\facade\Filesystem;


class IndexService extends Service
{
    public function home($user = null): Result
    {
        $res = $this->getIndexRes($user);
        $limit = 4;

        $param = ["page_size" => $limit, "is_show" => 1];
        //问题列表 2024年7月10日05:36:12不用了
        $forumService = new ForumService(app());
        $forumList = $forumService->list($param, $user);


        $res["forumList"] = $forumList->getData();
//        $res=[];
        //按区域统计特派员数量
        $res["expert_static"]=(new AuthenticationService(app()))->getStaticHome()->getData();
        return Result::success($res);
    }

    private function getIndexRes($user = null): array
    {
        $res = [];
        //找菜单
        $menu = [];
        $menu[] = ["name" => "科技资源库", "desc" => "", "url" => "/pages/home_modules/home_modules?id=23", "img" => getPublicDirUrl() . "/static/images/h5index/1.png"];
        $menu[] = ["name" => "科技成果库", "desc" => "", "url" => "/pages/home_modules/home_modules?id=1", "img" => getPublicDirUrl() . "/static/images/h5index/2.png"];
        $menu[] = ["name" => "科技需求库", "desc" => "", "url" => "/pages/answer/index", "img" => getPublicDirUrl() . "/static/images/h5index/3.png"];
//        $menu[] = ["name" => "县域创新基地", "desc" => "农业示范", "url" => "/pages/home_modules/home_modules?id=5", "img" => getPublicDirUrl() . "/static/images/h5index/4.png"];
        //2024年7月10日06:17:50 修改路径
        $menu[] = ["name" => "县域创新基地", "desc" => "农业示范", "url" => "/sub1/article/xianyu", "img" => getPublicDirUrl() . "/static/images/h5index/4.png"];
        $res["menu"] = $menu;
        $whereArticleClass = [];
        $whereArticleClass[] = ["is_index", "=", 1];
        $whereArticleClass[] = ["valid", "=", 1];
        //$whereArticleClass[] = ["parent_id", "=", 0];

        $articleClass = CmsArticleClass::with(["childrens"])->field("id,name,t_type")->where($whereArticleClass)->order("sort desc")->select();

        $articleService = new CmsArticleService(app());
        $limit = 4;
        $classList = [];
        foreach ($articleClass as $key => $value) {
            $class = $value->toArray();

            $param = [
                "class_id" => $class["id"],
                "limit" => $limit,
            ];
            $class["list"] = $articleService->topList($param, $user);
            $class["hasMore"] = 1;
            if (count($class["list"]) < $limit) {
                $class["hasMore"] = 0;
            }
            $classList[] = $class;
        }


        $res["classList"] = $classList;


        $whereArticleClass = [];
        $whereArticleClass[] = ["valid", "=", 1];
        $whereArticleClass[] = ["id", "=", 5];
        $articleClass = CmsArticleClass::where($whereArticleClass)->order("sort desc")->find();
        $res["indexClass"]["class"] = $articleClass;
        $param = [
            "class_id" => $articleClass->id,
            "limit" => $limit,
        ];
        $res["indexClass"]["list"] = $articleService->topList($param, $user);//县域创新基地ID
        return $res;
    }

    public function index($user = null): Result
    {

        $res = $this->getIndexRes($user);
        //2024年6月5日18:33:03 移除专家答疑
        /*$limit = 4;
        //2024年3月15日16:21:16 添加专家答疑
        $param = ["page_size" => $limit, "is_show" => 1];
        //专家库
        $forumService = new ForumService(app());
        $forumList = $forumService->list($param, $user);


        $res["classList"][] = [
            "id" => 0,
            "name" => "求助答疑",
            "t_type" => 0,
            "list" => $forumList->getData(),
            "hasMore" => 0,
        ];*/
        return Result::success($res);
    }

}
